package com.media.photocamera;

import android.app.Activity;
import android.content.Intent;


import com.media.photocamera.activity.CameraXActivity;
import com.media.photocamera.activity.CameraXFacePhotoActivity;
import com.media.photocamera.utils.CameraConstans;

import java.io.Serializable;
import java.lang.ref.WeakReference;

public class CameraStart implements Serializable {
    private int isFront = 1;
    private double scale = 0.75;
    private double widthScale = 0.8;
    private int light = 1;
    private int isSaveToAlbum;
    private int limit;
    private int count;
    private boolean isFaceCut = false;

    private WeakReference<Activity> mActivity;

    private CameraStart(CameraStartBuilder cameraStartBuilder) {
        this.isFront = cameraStartBuilder.isFront;
        this.scale = cameraStartBuilder.scale;
        this.widthScale = cameraStartBuilder.widthScale;
        this.light = cameraStartBuilder.light;
        this.isSaveToAlbum = cameraStartBuilder.isSaveToAlbum;
        this.limit = cameraStartBuilder.limit;
        this.count = cameraStartBuilder.count;
        this.isFaceCut = cameraStartBuilder.isFaceCut;
    }

    public void startActivityForResult(Activity activity, int requestCode) {
        mActivity = new WeakReference<>(activity);
        Intent intent = null;
        if(isFaceCut){
            intent =new Intent(mActivity.get(), CameraXFacePhotoActivity.class);
            intent.putExtra(CameraConstans.FACEPHOTO_scale, scale);
            intent.putExtra(CameraConstans.FACEPHOTO_widthScale, widthScale);
            intent.putExtra(CameraConstans.FACEPHOTO_light, light);
            intent.putExtra(CameraConstans.FACEPHOTO_isFront, isFront);
            intent.putExtra(CameraConstans.FACEPHOTO_isSaveToAlbum, isSaveToAlbum);
            intent.putExtra(CameraConstans.FACEPHOTO_limit, limit);
        }else{
            intent =new Intent(mActivity.get(), CameraXActivity.class);
            intent.putExtra(CameraConstans.FACEPHOTO_light, light);
            intent.putExtra(CameraConstans.FACEPHOTO_isFront, isFront);
            intent.putExtra(CameraConstans.FACEPHOTO_count, count);
            intent.putExtra(CameraConstans.FACEPHOTO_isSaveToAlbum, isSaveToAlbum);
            intent.putExtra(CameraConstans.FACEPHOTO_limit, limit);
        }
        mActivity.get().startActivityForResult(intent, requestCode);
    }

    public static class CameraStartBuilder {
        private int isFront = 1;
        private boolean isFaceCut = false;
        private double scale = 0.75;
        private double widthScale = 0.8;
        private int light = 1;
        private int isSaveToAlbum;
        private int limit;
        private int count;

        /**
         * 是否是人脸剪切
         * @param faceCut
         */
        public CameraStartBuilder setFaceCut(boolean faceCut) {
            isFaceCut = faceCut;
            return this;
        }

        /**
         * 是否默认前置
         *
         * @param isFront
         * @return
         */
        public CameraStartBuilder setIsFrontFirst(Boolean isFront) {
            this.isFront = isFront?1:0;
            return this;
        }

        /**
         * 人脸剪切宽高比
         *
         * @param scale
         * @return
         */
        public CameraStartBuilder setFaceScale(double scale) {
            this.scale = scale;
            return this;
        }

        /**
         * 人脸剪切屏幕宽度比
         *
         * @param widthScale
         * @return
         */
        public CameraStartBuilder setFaceWidthScale(double widthScale) {
            this.widthScale = widthScale;
            return this;
        }

        /**
         * 是否补光
         *
         * @param light
         * @return
         */
        public CameraStartBuilder setLight(boolean light) {
            this.light = light?1:0;
            return this;
        }

        /**
         * 拍摄的照片是否保存本地
         *
         * @param isSaveToAlbum
         * @return
         */
        public CameraStartBuilder setIsSaveToAlbum(boolean isSaveToAlbum) {
            this.isSaveToAlbum = isSaveToAlbum?1:0;
            return this;
        }

        /**
         * 设置文件大小限制
         *
         * @param limit
         * @return
         */
        public CameraStartBuilder setSizeLimit(int limit) {
            this.limit = limit;
            return this;
        }

        /**
         * 设置最大拍照数量
         *
         * @param count
         * @return
         */
        public CameraStartBuilder setMaxCount(int count) {
            this.count = count;
            return this;
        }

        public CameraStart build() {
            return new CameraStart(this);
        }
    }

}
